home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1W5BO5J (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  21.6 KB  |  861 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.swing.event.EventListenerList;
  6. import com.sun.java.swing.event.PopupMenuEvent;
  7. import com.sun.java.swing.event.PopupMenuListener;
  8. import com.sun.java.swing.plaf.PopupMenuUI;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.Dialog;
  12. import java.awt.Dimension;
  13. import java.awt.Frame;
  14. import java.awt.Graphics;
  15. import java.awt.GridBagConstraints;
  16. import java.awt.GridBagLayout;
  17. import java.awt.Insets;
  18. import java.awt.Point;
  19. import java.awt.Rectangle;
  20. import java.awt.Window;
  21. import java.awt.event.KeyEvent;
  22. import java.awt.event.MouseEvent;
  23. import java.beans.PropertyChangeListener;
  24. import java.io.IOException;
  25. import java.io.ObjectInputStream;
  26. import java.io.ObjectOutputStream;
  27. import java.io.Serializable;
  28. import java.util.Hashtable;
  29. import java.util.Vector;
  30.  
  31. public class JPopupMenu extends JComponent implements Accessible, MenuElement {
  32.    transient Component invoker;
  33.    transient Popup popup;
  34.    transient Frame frame;
  35.    private String label;
  36.    private boolean paintBorder;
  37.    private Insets margin;
  38.    private int desiredLocationX;
  39.    private int desiredLocationY;
  40.    private int lastPopupType;
  41.    private static final Object heavyPopupCacheKey = new StringBuffer("JPopupMenu.heavyPopupCache");
  42.    private static final Object lightPopupCacheKey = new StringBuffer("JPopupMenu.lightPopupCache");
  43.    private static final Object mediumPopupCacheKey = new StringBuffer("JPopupMenu.mediumPopupCache");
  44.    private static final Object defaultLWPopupEnabledKey = new StringBuffer("JPopupMenu.defaultLWPopupEnabledKey");
  45.    private static final int MAX_CACHE_SIZE = 5;
  46.    private transient MouseGrabber mouseGrabber;
  47.    private boolean lightWeightPopupEnabled;
  48.    private static final int LIGHT_WEIGHT_POPUP = 0;
  49.    private static final int MEDIUM_WEIGHT_POPUP = 1;
  50.    private static final int HEAVY_WEIGHT_POPUP = 2;
  51.    private SingleSelectionModel selectionModel;
  52.    static Class class$com$sun$java$swing$event$PopupMenuListener;
  53.  
  54.    public JPopupMenu() {
  55.       this((String)null);
  56.    }
  57.  
  58.    public JPopupMenu(String label) {
  59.       this.label = null;
  60.       this.paintBorder = true;
  61.       this.margin = null;
  62.       this.lastPopupType = 0;
  63.       this.lightWeightPopupEnabled = true;
  64.       this.label = label;
  65.       ((Container)this).setLayout(new GridBagLayout());
  66.       this.lightWeightPopupEnabled = getDefaultLightWeightPopupEnabled();
  67.       this.setSelectionModel(new DefaultSingleSelectionModel());
  68.       ((Component)this).addMouseListener(new 1());
  69.       this.updateUI();
  70.    }
  71.  
  72.    public JMenuItem add(Action a) {
  73.       JMenuItem mi = new JMenuItem((String)a.getValue("Name"), (Icon)a.getValue("SmallIcon"));
  74.       ((AbstractButton)mi).setHorizontalTextPosition(4);
  75.       ((AbstractButton)mi).setVerticalTextPosition(0);
  76.       mi.setEnabled(a.isEnabled());
  77.       ((AbstractButton)mi).addActionListener(a);
  78.       this.add(mi);
  79.       PropertyChangeListener actionPropertyChangeListener = this.createActionChangeListener(mi);
  80.       a.addPropertyChangeListener(actionPropertyChangeListener);
  81.       return mi;
  82.    }
  83.  
  84.    public JMenuItem add(JMenuItem menuItem) {
  85.       GridBagConstraints gbc = new GridBagConstraints();
  86.       gbc.fill = 2;
  87.       gbc.gridx = 0;
  88.       gbc.gridy = ((Container)this).getComponentCount() + 1;
  89.       super.add(menuItem, gbc);
  90.       return menuItem;
  91.    }
  92.  
  93.    public Component add(Component c) {
  94.       GridBagConstraints gbc = new GridBagConstraints();
  95.       gbc.fill = 2;
  96.       gbc.gridx = 0;
  97.       gbc.gridy = ((Container)this).getComponentCount() + 1;
  98.       super.add(c, gbc);
  99.       return c;
  100.    }
  101.  
  102.    public void addPopupMenuListener(PopupMenuListener l) {
  103.       EventListenerList var10000 = super.listenerList;
  104.       Class var10001 = class$com$sun$java$swing$event$PopupMenuListener;
  105.       if (var10001 == null) {
  106.          try {
  107.             var10001 = Class.forName("com.sun.java.swing.event.PopupMenuListener");
  108.          } catch (ClassNotFoundException var2) {
  109.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  110.          }
  111.  
  112.          class$com$sun$java$swing$event$PopupMenuListener = var10001;
  113.       }
  114.  
  115.       var10000.add(var10001, l);
  116.    }
  117.  
  118.    public void addSeparator() {
  119.       this.add((Component)(new JSeparator()));
  120.    }
  121.  
  122.    private boolean ancestorIsDialog(Component i) {
  123.       for(Container parent = i.getParent(); parent != null; parent = ((Component)parent).getParent()) {
  124.          if (parent instanceof Dialog) {
  125.             return true;
  126.          }
  127.       }
  128.  
  129.       return false;
  130.    }
  131.  
  132.    protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
  133.       return new ActionChangedListener(this, b);
  134.    }
  135.  
  136.    private Popup createHeavyWeightPopup() {
  137.       Frame frame = getFrame(this.invoker);
  138.       if (frame != null) {
  139.          this.popup = getRecycledHeavyPopup(frame);
  140.       } else {
  141.          frame = new Frame();
  142.       }
  143.  
  144.       if (this.popup == null) {
  145.          this.popup = new WindowPopup(this, frame);
  146.       }
  147.  
  148.       return this.popup;
  149.    }
  150.  
  151.    private Popup createLightWeightPopup() {
  152.       Popup popup = getRecycledLightPopup();
  153.       if (popup == null) {
  154.          popup = new JPanelPopup(this);
  155.       }
  156.  
  157.       return (Popup)popup;
  158.    }
  159.  
  160.    private Popup createMediumWeightPopup() {
  161.       Popup popup = getRecycledMediumPopup();
  162.       if (popup == null) {
  163.          popup = new PanelPopup(this);
  164.       }
  165.  
  166.       return (Popup)popup;
  167.    }
  168.  
  169.    protected void firePopupMenuCanceled() {
  170.       Object[] listeners = super.listenerList.getListenerList();
  171.       PopupMenuEvent e = null;
  172.  
  173.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  174.          Object var10000 = listeners[i];
  175.          Class var10001 = class$com$sun$java$swing$event$PopupMenuListener;
  176.          if (var10001 == null) {
  177.             try {
  178.                var10001 = Class.forName("com.sun.java.swing.event.PopupMenuListener");
  179.             } catch (ClassNotFoundException var4) {
  180.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  181.             }
  182.  
  183.             class$com$sun$java$swing$event$PopupMenuListener = var10001;
  184.          }
  185.  
  186.          if (var10000 == var10001) {
  187.             if (e == null) {
  188.                e = new PopupMenuEvent(this);
  189.             }
  190.  
  191.             ((PopupMenuListener)listeners[i + 1]).popupMenuCanceled(e);
  192.          }
  193.       }
  194.  
  195.    }
  196.  
  197.    protected void firePopupMenuWillBecomeInvisible() {
  198.       Object[] listeners = super.listenerList.getListenerList();
  199.       PopupMenuEvent e = null;
  200.  
  201.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  202.          Object var10000 = listeners[i];
  203.          Class var10001 = class$com$sun$java$swing$event$PopupMenuListener;
  204.          if (var10001 == null) {
  205.             try {
  206.                var10001 = Class.forName("com.sun.java.swing.event.PopupMenuListener");
  207.             } catch (ClassNotFoundException var4) {
  208.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  209.             }
  210.  
  211.             class$com$sun$java$swing$event$PopupMenuListener = var10001;
  212.          }
  213.  
  214.          if (var10000 == var10001) {
  215.             if (e == null) {
  216.                e = new PopupMenuEvent(this);
  217.             }
  218.  
  219.             ((PopupMenuListener)listeners[i + 1]).popupMenuWillBecomeInvisible(e);
  220.          }
  221.       }
  222.  
  223.    }
  224.  
  225.    protected void firePopupMenuWillBecomeVisible() {
  226.       Object[] listeners = super.listenerList.getListenerList();
  227.       PopupMenuEvent e = null;
  228.  
  229.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  230.          Object var10000 = listeners[i];
  231.          Class var10001 = class$com$sun$java$swing$event$PopupMenuListener;
  232.          if (var10001 == null) {
  233.             try {
  234.                var10001 = Class.forName("com.sun.java.swing.event.PopupMenuListener");
  235.             } catch (ClassNotFoundException var4) {
  236.                throw new NoClassDefFoundError(((Throwable)var4).getMessage());
  237.             }
  238.  
  239.             class$com$sun$java$swing$event$PopupMenuListener = var10001;
  240.          }
  241.  
  242.          if (var10000 == var10001) {
  243.             if (e == null) {
  244.                e = new PopupMenuEvent(this);
  245.             }
  246.  
  247.             ((PopupMenuListener)listeners[i + 1]).popupMenuWillBecomeVisible(e);
  248.          }
  249.       }
  250.  
  251.    }
  252.  
  253.    public AccessibleContext getAccessibleContext() {
  254.       if (super.accessibleContext == null) {
  255.          super.accessibleContext = new AccessibleJPopupMenu(this);
  256.       }
  257.  
  258.       return super.accessibleContext;
  259.    }
  260.  
  261.    public Component getComponent() {
  262.       return this;
  263.    }
  264.  
  265.    public Component getComponentAtIndex(int i) {
  266.       int ncomponents = ((Container)this).getComponentCount();
  267.       if (i <= ncomponents) {
  268.          Component[] component = ((Container)this).getComponents();
  269.          return component[i];
  270.       } else {
  271.          return null;
  272.       }
  273.    }
  274.  
  275.    public int getComponentIndex(Component c) {
  276.       int ncomponents = ((Container)this).getComponentCount();
  277.       Component[] component = ((Container)this).getComponents();
  278.  
  279.       for(int i = 0; i < ncomponents; ++i) {
  280.          Component comp = component[i];
  281.          if (comp == c) {
  282.             return i;
  283.          }
  284.       }
  285.  
  286.       return -1;
  287.    }
  288.  
  289.    public static boolean getDefaultLightWeightPopupEnabled() {
  290.       Boolean b = (Boolean)SwingUtilities.appContextGet(defaultLWPopupEnabledKey);
  291.       if (b == null) {
  292.          SwingUtilities.appContextPut(defaultLWPopupEnabledKey, Boolean.TRUE);
  293.          return true;
  294.       } else {
  295.          return b;
  296.       }
  297.    }
  298.  
  299.    private static Frame getFrame(Component c) {
  300.       Component w;
  301.       for(w = c; !(w instanceof Frame) && w != null; w = w.getParent()) {
  302.       }
  303.  
  304.       return (Frame)w;
  305.    }
  306.  
  307.    private static Hashtable getHeavyPopupCache() {
  308.       Hashtable cache = (Hashtable)SwingUtilities.appContextGet(heavyPopupCacheKey);
  309.       if (cache == null) {
  310.          cache = new Hashtable(2);
  311.          SwingUtilities.appContextPut(heavyPopupCacheKey, cache);
  312.       }
  313.  
  314.       return cache;
  315.    }
  316.  
  317.    public Component getInvoker() {
  318.       return this.invoker;
  319.    }
  320.  
  321.    public String getLabel() {
  322.       return this.label;
  323.    }
  324.  
  325.    private static Vector getLightPopupCache() {
  326.       Vector cache = (Vector)SwingUtilities.appContextGet(lightPopupCacheKey);
  327.       if (cache == null) {
  328.          cache = new Vector();
  329.          SwingUtilities.appContextPut(lightPopupCacheKey, cache);
  330.       }
  331.  
  332.       return cache;
  333.    }
  334.  
  335.    public Insets getMargin() {
  336.       return this.margin == null ? new Insets(0, 0, 0, 0) : this.margin;
  337.    }
  338.  
  339.    private static Vector getMediumPopupCache() {
  340.       Vector cache = (Vector)SwingUtilities.appContextGet(mediumPopupCacheKey);
  341.       if (cache == null) {
  342.          cache = new Vector();
  343.          SwingUtilities.appContextPut(mediumPopupCacheKey, cache);
  344.       }
  345.  
  346.       return cache;
  347.    }
  348.  
  349.    static synchronized Popup getRecycledHeavyPopup(Frame f) {
  350.       Hashtable heavyPopupCache = getHeavyPopupCache();
  351.       if (heavyPopupCache.containsKey(f)) {
  352.          Vector cache = (Vector)heavyPopupCache.get(f);
  353.          if (cache.size() > 0) {
  354.             Popup r = (Popup)cache.elementAt(0);
  355.             cache.removeElementAt(0);
  356.             return r;
  357.          } else {
  358.             return null;
  359.          }
  360.       } else {
  361.          return null;
  362.       }
  363.    }
  364.  
  365.    static synchronized Popup getRecycledLightPopup() {
  366.       Vector lightPopupCache = getLightPopupCache();
  367.       if (lightPopupCache.size() > 0) {
  368.          Popup r = (Popup)lightPopupCache.elementAt(0);
  369.          lightPopupCache.removeElementAt(0);
  370.          return r;
  371.       } else {
  372.          return null;
  373.       }
  374.    }
  375.  
  376.    static synchronized Popup getRecycledMediumPopup() {
  377.       Vector mediumPopupCache = getMediumPopupCache();
  378.       if (mediumPopupCache.size() > 0) {
  379.          Popup r = (Popup)mediumPopupCache.elementAt(0);
  380.          mediumPopupCache.removeElementAt(0);
  381.          return r;
  382.       } else {
  383.          return null;
  384.       }
  385.    }
  386.  
  387.    JPopupMenu getRootPopupMenu() {
  388.       JPopupMenu mp;
  389.       for(mp = this; mp != null && !mp.isPopupMenu() && mp.getInvoker() != null && mp.getInvoker().getParent() != null && mp.getInvoker().getParent() instanceof JPopupMenu; mp = (JPopupMenu)mp.getInvoker().getParent()) {
  390.       }
  391.  
  392.       return mp;
  393.    }
  394.  
  395.    public SingleSelectionModel getSelectionModel() {
  396.       return this.selectionModel;
  397.    }
  398.  
  399.    public MenuElement[] getSubElements() {
  400.       Vector tmp = new Vector();
  401.       int c = ((Container)this).getComponentCount();
  402.  
  403.       for(int i = 0; i < c; ++i) {
  404.          Component m = ((Container)this).getComponent(i);
  405.          if (m instanceof MenuElement) {
  406.             tmp.addElement(m);
  407.          }
  408.       }
  409.  
  410.       MenuElement[] result = new MenuElement[tmp.size()];
  411.       int var7 = 0;
  412.  
  413.       for(int var6 = tmp.size(); var7 < var6; ++var7) {
  414.          result[var7] = (MenuElement)tmp.elementAt(var7);
  415.       }
  416.  
  417.       return result;
  418.    }
  419.  
  420.    public PopupMenuUI getUI() {
  421.       return (PopupMenuUI)super.ui;
  422.    }
  423.  
  424.    public String getUIClassID() {
  425.       return "PopupMenuUI";
  426.    }
  427.  
  428.    private static Window getWindow(Component c) {
  429.       Component w;
  430.       for(w = c; !(w instanceof Window) && w != null; w = w.getParent()) {
  431.       }
  432.  
  433.       return (Window)w;
  434.    }
  435.  
  436.    public void insert(Action a, int index) {
  437.       throw new Error("void insert(Action, int) {} not yet implemented");
  438.    }
  439.  
  440.    public void insert(Component component, int index) {
  441.       if (index < 0) {
  442.          throw new IllegalArgumentException("index less than zero.");
  443.       } else {
  444.          int nitems = ((Container)this).getComponentCount();
  445.          Vector tempItems = new Vector();
  446.  
  447.          for(int i = index; i < nitems; ++i) {
  448.             tempItems.addElement(((Container)this).getComponent(index));
  449.             ((Container)this).remove(index);
  450.          }
  451.  
  452.          this.add(component);
  453.  
  454.          for(int i = 0; i < tempItems.size(); ++i) {
  455.             this.add((Component)tempItems.elementAt(i));
  456.          }
  457.  
  458.       }
  459.    }
  460.  
  461.    public boolean isBorderPainted() {
  462.       return this.paintBorder;
  463.    }
  464.  
  465.    public boolean isLightWeightPopupEnabled() {
  466.       return this.lightWeightPopupEnabled;
  467.    }
  468.  
  469.    private boolean isPopupMenu() {
  470.       return this.invoker != null && !(this.invoker instanceof JMenu);
  471.    }
  472.  
  473.    boolean isSubPopupMenu(JPopupMenu popup) {
  474.       int ncomponents = ((Container)this).getComponentCount();
  475.       Component[] component = ((Container)this).getComponents();
  476.  
  477.       for(int i = 0; i < ncomponents; ++i) {
  478.          Component comp = component[i];
  479.          if (comp instanceof JMenu) {
  480.             JMenu menu = (JMenu)comp;
  481.             JPopupMenu subPopup = menu.getPopupMenu();
  482.             if (subPopup == popup) {
  483.                return true;
  484.             }
  485.  
  486.             if (subPopup.isSubPopupMenu(popup)) {
  487.                return true;
  488.             }
  489.          }
  490.       }
  491.  
  492.       return false;
  493.    }
  494.  
  495.    public boolean isVisible() {
  496.       return this.popup != null ? this.popup.isShowing() : false;
  497.    }
  498.  
  499.    public void menuSelectionChanged(boolean isIncluded) {
  500.       if (this.invoker instanceof JMenu) {
  501.          JMenu m = (JMenu)this.invoker;
  502.          if (isIncluded) {
  503.             m.setPopupMenuVisible(true);
  504.          } else {
  505.             m.setPopupMenuVisible(false);
  506.          }
  507.       }
  508.  
  509.       if (this.isPopupMenu() && !isIncluded) {
  510.          this.setVisible(false);
  511.       }
  512.  
  513.    }
  514.  
  515.    public void pack() {
  516.       if (this.popup != null) {
  517.          this.popup.pack();
  518.       }
  519.  
  520.    }
  521.  
  522.    protected void paintBorder(Graphics g) {
  523.       if (this.isBorderPainted()) {
  524.          super.paintBorder(g);
  525.       }
  526.  
  527.    }
  528.  
  529.    private boolean popupFit(Rectangle popupRectInScreen) {
  530.       if (this.invoker != null) {
  531.          for(Container parent = this.invoker.getParent(); parent != null; parent = ((Component)parent).getParent()) {
  532.             if (parent instanceof JFrame || parent instanceof JDialog) {
  533.                return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
  534.             }
  535.  
  536.             if (parent instanceof JApplet) {
  537.                Rectangle r = ((Component)parent).getBounds();
  538.                Point p = ((Component)parent).getLocationOnScreen();
  539.                r.x = p.x;
  540.                r.y = p.y;
  541.                return SwingUtilities.isRectangleContainingRectangle(r, popupRectInScreen);
  542.             }
  543.  
  544.             if (parent instanceof Frame) {
  545.                return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
  546.             }
  547.          }
  548.       }
  549.  
  550.       return false;
  551.    }
  552.  
  553.    public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
  554.    }
  555.  
  556.    public void processMouseEvent(MouseEvent event, MenuElement[] path, MenuSelectionManager manager) {
  557.    }
  558.  
  559.    private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
  560.       s.defaultReadObject();
  561.       Vector values = (Vector)s.readObject();
  562.       int indexCounter = 0;
  563.       int maxCounter = values.size();
  564.       if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("invoker")) {
  565.          ++indexCounter;
  566.          this.invoker = (Component)values.elementAt(indexCounter);
  567.          ++indexCounter;
  568.       }
  569.  
  570.       if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("popup")) {
  571.          ++indexCounter;
  572.          this.popup = (Popup)values.elementAt(indexCounter);
  573.          ++indexCounter;
  574.       }
  575.  
  576.       if (indexCounter < maxCounter && values.elementAt(indexCounter).equals("frame")) {
  577.          ++indexCounter;
  578.          this.frame = (Frame)values.elementAt(indexCounter);
  579.          ++indexCounter;
  580.       }
  581.  
  582.    }
  583.  
  584.    static synchronized void recycleHeavyPopup(Popup aPopup) {
  585.       Frame f = getFrame((Component)aPopup);
  586.       Hashtable heavyPopupCache = getHeavyPopupCache();
  587.       Vector cache;
  588.       if (heavyPopupCache.containsKey(f)) {
  589.          cache = (Vector)heavyPopupCache.get(f);
  590.       } else {
  591.          cache = new Vector();
  592.          heavyPopupCache.put(f, cache);
  593.          ((Window)f).addWindowListener(new 2(f));
  594.       }
  595.  
  596.       if (cache.size() < 5) {
  597.          cache.addElement(aPopup);
  598.       }
  599.  
  600.    }
  601.  
  602.    static synchronized void recycleLightPopup(Popup aPopup) {
  603.       Vector lightPopupCache = getLightPopupCache();
  604.       if (lightPopupCache.size() < 5) {
  605.          lightPopupCache.addElement(aPopup);
  606.       }
  607.  
  608.    }
  609.  
  610.    static synchronized void recycleMediumPopup(Popup aPopup) {
  611.       Vector mediumPopupCache = getMediumPopupCache();
  612.       if (mediumPopupCache.size() < 5) {
  613.          mediumPopupCache.addElement(aPopup);
  614.       }
  615.  
  616.    }
  617.  
  618.    static void recyclePopup(Popup aPopup) {
  619.       if (aPopup instanceof JPanelPopup) {
  620.          recycleLightPopup(aPopup);
  621.       } else if (aPopup instanceof WindowPopup) {
  622.          recycleHeavyPopup(aPopup);
  623.       } else if (aPopup instanceof PanelPopup) {
  624.          recycleMediumPopup(aPopup);
  625.       }
  626.  
  627.    }
  628.  
  629.    public void removePopupMenuListener(PopupMenuListener l) {
  630.       EventListenerList var10000 = super.listenerList;
  631.       Class var10001 = class$com$sun$java$swing$event$PopupMenuListener;
  632.       if (var10001 == null) {
  633.          try {
  634.             var10001 = Class.forName("com.sun.java.swing.event.PopupMenuListener");
  635.          } catch (ClassNotFoundException var2) {
  636.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  637.          }
  638.  
  639.          class$com$sun$java$swing$event$PopupMenuListener = var10001;
  640.       }
  641.  
  642.       var10000.remove(var10001, l);
  643.    }
  644.  
  645.    private void replacePopup(int newType) {
  646.       this.popup.removeComponent(this);
  647.       recyclePopup(this.popup);
  648.       switch (newType) {
  649.          case 0:
  650.             this.popup = this.createLightWeightPopup();
  651.             break;
  652.          case 1:
  653.             this.popup = this.createMediumWeightPopup();
  654.             break;
  655.          case 2:
  656.             this.popup = this.createHeavyWeightPopup();
  657.       }
  658.  
  659.       this.popup.setLocationOnScreen(this.desiredLocationX, this.desiredLocationY);
  660.       this.popup.addComponent(this, "Center");
  661.       ((Container)this).invalidate();
  662.       this.popup.setBackground(((Component)this).getBackground());
  663.       this.popup.pack();
  664.    }
  665.  
  666.    private void requestAddGrab(Component invoker, Popup aPopup) {
  667.       if (this.mouseGrabber == null) {
  668.          this.mouseGrabber = new MouseGrabber(this);
  669.       }
  670.  
  671.       Window ancestor = getWindow(invoker);
  672.       this.mouseGrabber.grabContainer(ancestor, aPopup.getComponent());
  673.    }
  674.  
  675.    private void requestRemoveGrab(Component invoker) {
  676.       if (this.mouseGrabber != null) {
  677.          this.mouseGrabber.ungrabContainers();
  678.       }
  679.  
  680.    }
  681.  
  682.    public void setBorderPainted(boolean b) {
  683.       this.paintBorder = b;
  684.       ((Component)this).repaint();
  685.    }
  686.  
  687.    public static void setDefaultLightWeightPopupEnabled(boolean aFlag) {
  688.       SwingUtilities.appContextPut(defaultLWPopupEnabledKey, new Boolean(aFlag));
  689.    }
  690.  
  691.    public void setInvoker(Component invoker) {
  692.       Component oldInvoker = this.invoker;
  693.       this.invoker = invoker;
  694.       if (oldInvoker != this.invoker && super.ui != null) {
  695.          super.ui.uninstallUI(this);
  696.          super.ui.installUI(this);
  697.       }
  698.  
  699.       ((Container)this).invalidate();
  700.    }
  701.  
  702.    public void setLabel(String label) {
  703.       this.label = label;
  704.       ((JComponent)this).firePropertyChange("label", label, label);
  705.       if (super.accessibleContext != null) {
  706.          super.accessibleContext.firePropertyChange("AccessibleVisibleData", label, label);
  707.       }
  708.  
  709.       ((Container)this).invalidate();
  710.       ((Component)this).repaint();
  711.    }
  712.  
  713.    public void setLightWeightPopupEnabled(boolean aFlag) {
  714.       this.lightWeightPopupEnabled = aFlag;
  715.    }
  716.  
  717.    public void setLocation(int x, int y) {
  718.       if (this.popup != null) {
  719.          this.popup.setLocationOnScreen(x, y);
  720.       } else {
  721.          this.desiredLocationX = x;
  722.          this.desiredLocationY = y;
  723.       }
  724.  
  725.    }
  726.  
  727.    public void setPopupSize(int width, int height) {
  728.       this.popup.setSize(width, height);
  729.    }
  730.  
  731.    public void setPopupSize(Dimension d) {
  732.       this.popup.setSize(d.width, d.height);
  733.    }
  734.  
  735.    public void setSelected(Component sel) {
  736.       SingleSelectionModel model = this.getSelectionModel();
  737.       int index = this.getComponentIndex(sel);
  738.       model.setSelectedIndex(index);
  739.    }
  740.  
  741.    public void setSelectionModel(SingleSelectionModel model) {
  742.       this.selectionModel = model;
  743.    }
  744.  
  745.    public void setUI(PopupMenuUI ui) {
  746.       super.setUI(ui);
  747.    }
  748.  
  749.    public void setVisible(boolean b) {
  750.       if (b != this.isVisible()) {
  751.          if (!b) {
  752.             this.getSelectionModel().clearSelection();
  753.             if (this.getRootPopupMenu() == this) {
  754.                this.requestRemoveGrab(this.invoker);
  755.             }
  756.          } else if (this.isPopupMenu() && this.getSubElements().length > 0) {
  757.             MenuElement[] me = new MenuElement[2];
  758.             me[0] = this;
  759.             me[1] = this.getSubElements()[0];
  760.             MenuSelectionManager.defaultManager().setSelectedPath(me);
  761.          }
  762.  
  763.          if (b) {
  764.             boolean shouldDowngradeHeavyWeight = this.ancestorIsDialog(this.invoker);
  765.             this.firePopupMenuWillBecomeVisible();
  766.             switch (this.lastPopupType) {
  767.                case 0:
  768.                   this.popup = this.createLightWeightPopup();
  769.                   break;
  770.                case 1:
  771.                   this.popup = this.createMediumWeightPopup();
  772.                   break;
  773.                case 2:
  774.                   this.popup = this.createHeavyWeightPopup();
  775.             }
  776.  
  777.             int popupType = this.lastPopupType;
  778.             this.popup.setLocationOnScreen(this.desiredLocationX, this.desiredLocationY);
  779.             this.popup.addComponent(this, "Center");
  780.             this.popup.setBackground(((Component)this).getBackground());
  781.             this.popup.pack();
  782.             Rectangle popupRect = new Rectangle(this.desiredLocationX, this.desiredLocationY, this.popup.getWidth(), this.popup.getHeight());
  783.             byte newPopupType;
  784.             if (this.popupFit(popupRect)) {
  785.                if (this.lightWeightPopupEnabled) {
  786.                   newPopupType = 0;
  787.                } else {
  788.                   newPopupType = 1;
  789.                }
  790.             } else if (shouldDowngradeHeavyWeight) {
  791.                newPopupType = 1;
  792.             } else {
  793.                newPopupType = 2;
  794.             }
  795.  
  796.             if (newPopupType != popupType) {
  797.                this.replacePopup(newPopupType);
  798.                popupType = newPopupType;
  799.             }
  800.  
  801.             this.lastPopupType = popupType;
  802.             this.popup.show(this.invoker);
  803.             if (this.getRootPopupMenu() == this) {
  804.                this.requestAddGrab(this.invoker, this.popup);
  805.             }
  806.          } else if (this.popup != null) {
  807.             this.firePopupMenuWillBecomeInvisible();
  808.             this.popup.hide();
  809.             this.popup.removeComponent(this);
  810.             recyclePopup(this.popup);
  811.             this.popup = null;
  812.          }
  813.  
  814.       }
  815.    }
  816.  
  817.    public void show(Component invoker, int x, int y) {
  818.       this.setInvoker(invoker);
  819.       Frame newFrame = getFrame(invoker);
  820.       if (newFrame != this.frame && newFrame != null) {
  821.          this.frame = newFrame;
  822.          if (this.popup != null) {
  823.             this.setVisible(false);
  824.          }
  825.       }
  826.  
  827.       Point invokerOrigin = invoker.getLocationOnScreen();
  828.       this.setLocation(invokerOrigin.x + x, invokerOrigin.y + y);
  829.       this.setVisible(true);
  830.    }
  831.  
  832.    public void updateUI() {
  833.       this.setUI((PopupMenuUI)UIManager.getUI(this));
  834.    }
  835.  
  836.    private void writeObject(ObjectOutputStream s) throws IOException {
  837.       Vector values = new Vector();
  838.       s.defaultWriteObject();
  839.       if (this.invoker != null && this.invoker instanceof Serializable) {
  840.          values.addElement("invoker");
  841.          values.addElement(this.invoker);
  842.       }
  843.  
  844.       if (this.popup != null && this.popup instanceof Serializable) {
  845.          values.addElement("popup");
  846.          values.addElement(this.popup);
  847.       }
  848.  
  849.       if (this.frame != null && this.frame instanceof Serializable) {
  850.          values.addElement("frame");
  851.          values.addElement(this.frame);
  852.       }
  853.  
  854.       s.writeObject(values);
  855.    }
  856.  
  857.    static Hashtable access$getHeavyPopupCache() {
  858.       return getHeavyPopupCache();
  859.    }
  860. }
  861.